//
//  ESUNAlertView.m
//  ESUN
//
//  Created by wang yongfeng on 13-12-4.
//  Copyright (c) 2013年 wang yongfeng. All rights reserved.
//

#import "ESUNAlertView.h"
#import <QuartzCore/QuartzCore.h>
@implementation ESUNAlertView
@synthesize alertString;
@synthesize delegate;
@synthesize color;
@synthesize font;
@synthesize bgImageView;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        color = [UIColor whiteColor];
        font = [UIFont fontWithName:FONT_NAME size:13];
        bgImageView = [[UIImageView alloc] init];
    }
    return self;
}

- (ESUNAlertView *)initWithImage:(UIImage *)image withDelegate:(UIViewController *)delegate
{
    self = [super init];
    if(self)
    {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        [self setFrame:CGRectMake(0, 0, image.size.width/2.0, image.size.height/2.0)];
        self.delegate = delegate;
        [bgImageView setFrame:CGRectMake(0, 0, image.size.width/2.0, image.size.height/2.0)];
        [self.bgImageView setImage:image];
    }
    return self;
}

- (ESUNAlertView *)initWithTitle:(NSString *)title withDelegate:(UIViewController *)delegate
{
    self = [super init];
    if(self)
    {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        CGSize size = [title sizeWithAttributes:@{NSFontAttributeName:font}];
        [self setFrame:CGRectMake(0, 0, size.width + 20,30)];
        [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];
        self.delegate = delegate;
        self.alertString = title;
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self addSubview:self.bgImageView];
    if(self.alertString != nil)
    {
        UILabel *title = [[UILabel alloc] initWithFrame: self.bounds];
        title.text = self.alertString;
        title.textAlignment = NSTextAlignmentCenter;
        title.font = self.font;
        title.textColor = self.color;
        [title setBackgroundColor:[UIColor clearColor]];
        [self addSubview:title];
    }
    self.center =  CGPointMake(self.delegate.view.center.x,self.delegate.view.frame.size.height - 75);
}

- (void)show
{
    [self.delegate.view addSubview:self];
    [UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
}

@end
